//
//  MoreBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/30.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class MoreBodyView: UIView {
    
    // scrollView
    private var scrollView = UIScrollView()
    
    // MARK: 赋值
    public var ticketInfos:[SpecialTicketInfo]? {
        didSet {
            updateLayout()
        }
    }
    // MARK: 回调
    public var complete:((_ item:SpecialTicketInfo) -> Void)?
    
    override func layoutSubviews() {
        guard let tickets = ticketInfos else {
            return
        }
        // 数量
        let columns = 2
        // 宽度
        let btnW = (SCREEN_WIDTH - MARGIN * 4) * 0.5
        // 高度
        let btnH = MARGIN80
        // 间距
        let margin = (frame.width - CGFloat(columns) * btnW) / CGFloat(columns + 1)
        // 记录最后一个View高度
        var tempHeight:CGFloat = 0
        // 开始摆放
        for i in 0..<tickets.count {
            let ui = SubMoreBodyView()
            ui.tag = i
            ui.backgroundColor = UIColor.white
            ui.layer.cornerRadius = 5
            ui.layer.masksToBounds = true
            // 赋值
            ui.ticket = tickets[i]
            // 坐标
            let btnX = CGFloat(i % columns) * (margin * 3 + btnW)
            let btnY = CGFloat(i / columns) * (margin * 2 + btnH)
            ui.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            scrollView.addSubview(ui)
            // 最后一个View
            if i == tickets.count - 1 {
                tempHeight = ui.frame.maxY + MARGIN50
            }
            // 添加点击事件
            ui.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(tap:))))
        }
        // 设置scrollView
        addSubview(scrollView)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentSize = CGSize(width: frame.width, height: tempHeight)
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
    }
    
    @objc func clickItem(tap:UITapGestureRecognizer) -> Void {
        if let callback = complete,let index = tap.view?.tag {
            callback(ticketInfos![index])
        }
    }
    
    class SubMoreBodyView: UIView {
        
        public var ticket:SpecialTicketInfo?
        
        override func layoutSubviews() {
            // 出发城市
            let startCityView = UILabel(title: "测试", color: UIColor.black, size: 15)
            startCityView.frame = CGRect(x: MARGIN10, y: MARGIN, width: MARGIN50, height: MARGIN)
            addSubview(startCityView)
            // 箭头
            let arrowView = UIImageView(image: UIImage(named: "checkinarrow"))
            let arrowX = (frame.width - arrowView.bounds.width) * 0.5
            arrowView.frame.origin = CGPoint(x: arrowX, y: MARGIN)
            addSubview(arrowView)
            // 到达城市
            let stopCityView = UILabel(title: "测试", color: UIColor.black, size: 15,align: .right)
            let stopX = frame.width - MARGIN60
            stopCityView.frame = CGRect(x: stopX, y: MARGIN, width: MARGIN50, height: MARGIN)
            addSubview(stopCityView)
            // 日期
            let dateView = UILabel(title: "2020-02-12", color: UIColor.gray, size: 13)
            let dateY = startCityView.frame.maxY + MARGIN
            dateView.frame = CGRect(x: MARGIN10, y: dateY, width: MARGIN90, height: MARGIN)
            addSubview(dateView)
            // 票价
            let priceView = UILabel(title: "¥1888起", color: COLOR_RED, size: 13,align: .right)
            priceView.frame = CGRect(x: stopX, y: dateY, width: MARGIN50, height: MARGIN)
            addSubview(priceView)
            // 赋值
            if ticket != nil {
                startCityView.text = ticket?.city
                stopCityView.text = ticket?.endcity
                dateView.text = ticket?.departdate
                priceView.text = "¥\(ticket!.ticketprice)"
            }
        }
    }
}
